package fhp.hlhj.giantfold.activity.login

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import android.transition.Fade
import android.transition.Slide
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.framework.ShareSDK
import cn.sharesdk.tencent.qq.QQ
import cn.sharesdk.wechat.friends.Wechat
import com.google.gson.Gson
import com.lzy.okgo.model.HttpParams
import com.mob.MobSDK
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.user.BindPhoneAty
import fhp.hlhj.giantfold.customView.RuleDialog
import fhp.hlhj.giantfold.event.RecreateEvent
import fhp.hlhj.giantfold.event.RefreshLoginEvent
import fhp.hlhj.giantfold.interfaces.IWeChatLogin
import fhp.hlhj.giantfold.javaBean.WxBean
import fhp.hlhj.giantfold.module.OtherLoginBean
import fhp.hlhj.giantfold.presenter.WechatLoginPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.SPUtils
import kotlinx.android.synthetic.main.activity_wechat_login_aty.*
import org.greenrobot.eventbus.EventBus
import java.util.HashMap

class WechatLoginAty : BaseActivity(), IWeChatLogin {
    private var a=0;
    private lateinit var presenter:WechatLoginPresenter
    private lateinit var wx:Platform
    private lateinit var qq:Platform
    override fun getContentId(): Int = R.layout.activity_wechat_login_aty
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
//        MobSDK.init(this,"23c93100d7a37","55bd2aa9d0671bb3068a00833516c2c5")
        presenter= WechatLoginPresenter(this)
        window.enterTransition = Slide().setDuration(800)
        window.returnTransition =Fade().setDuration(200)
        window.exitTransition =Fade().setDuration(200)
        setPlatForm()
    }

    private fun setPlatForm() {
        wx = ShareSDK.getPlatform(Wechat.NAME)
        qq = ShareSDK.getPlatform(QQ.NAME)

        wx.platformActionListener=object :PlatformActionListener{
            override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
                LogUtil.log("微信登录${a}次陈宫"+p0?.db?.exportData())
                val wxBean = Gson().fromJson(p0?.db?.exportData(), WxBean::class.java)
                Contents.wxBean= wxBean
                /*             Contents.loginType=1
                             Contents.isLogin=true
                             SPUtils.saveUserIsLogin(this@WechatLoginAty,true)
                             SPUtils.saveUser(this@WechatLoginAty,wxBean)
                             EventBus.getDefault().postSticky(RefreshLoginEvent())*/
                val params = HttpParams()
                params.put("openid",wxBean.openid)
                params.put("third_key",Contents.third_Key)
                params.put("login_type",0)
                presenter.loginWx(params)
            }
            override fun onCancel(p0: Platform?, p1: Int) {
                LogUtil.log("取消")
            }
            override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                LogUtil.log("错误")
            }
        }
    }
    @SuppressLint("RestrictedApi")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initListener() {
        btExit.setOnClickListener { finish() }
        btOther.setOnClickListener {
            val intent = Intent(this, OtherLoginAty::class.java)
            startActivityForResult(intent,1,ActivityOptions.makeSceneTransitionAnimation(this,img,"img").toBundle())
        }

        btWCLogin.setOnClickListener {
            a++
            LogUtil.log("微信登录---${a}次")
            wx.authorize()
        }
        btUnlogin.setOnClickListener {
            RuleDialog("接天莲叶无穷碧",this).show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=Activity.RESULT_OK)return
        if (requestCode==1){
            setResult(-1)
            finish()
        }
    }

    override fun showLoading() {

    }
    override fun login(otherLoginBean: OtherLoginBean) {
        if (otherLoginBean.code==200){
            MyUtils.toast("登录成功")
            Contents.userId=otherLoginBean.data.id
            if (Contents.userId.equals("0")){
                startActivity(Intent(this,BindPhoneAty::class.java))
            }
            Contents.isLogin=true
            SPUtils.saveOpenId(this,Contents.wxBean?.openid)
            SPUtils.saveLoginType(this,1)
            EventBus.getDefault().postSticky(RefreshLoginEvent())
            EventBus.getDefault().postSticky(RecreateEvent())
            this.setResult(-1)
            finish()
        }
    }
}
